package com.example.myapplication.utils

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import kotlin.apply

object DisplayUtils {
    /**
     * Dip2px
     *
     * @param context
     * @param dipValue
     * @return
     */
    fun dip2px(context: Context, dipValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dipValue * scale + 0.5f).toInt()
    }

    fun px2dp(context: Context, px: Float): Int {
        val density = context.resources.displayMetrics.density
        return (px / density + 0.5f).toInt()
    }

    @SuppressLint("InternalInsetResource")
    fun getStatusBarHeight(context: Context): Int {
        var result = 0
        var resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = context.resources.getDimensionPixelSize(resourceId)
        }
        return result
    }

    fun getWidth(context: Context): Int {
        return context.resources.displayMetrics.widthPixels
    }

    fun getHeight(context: Context): Int {
        return context.resources.displayMetrics.heightPixels
    }

    fun removeFromParent(view: View?) {
        view?.apply {
            if (parent is ViewGroup) {
                (parent as ViewGroup).removeView(view)
            }
        }
    }
}